package com.suo.web;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "cookieServlet", value = "/cookieServlet")
public class CookieServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Cookie[] cookies = request.getCookies();

        String username = "";
        String password = "";

        for(Cookie cookie : cookies) {
            if("username".equals(cookie.getName())) {
                username = cookie.getValue();

            }
        }

        for(Cookie cookie : cookies) {
            if("password".equals(cookie.getName())) {
                password = cookie.getValue();

            }
        }
        if(!"".equals(username) && !"".equals(password)) {
            String s = "{\"name\":\""+username+"\",\"password\":\""+password+"\"}";

            response.getWriter().write(s);
        }

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
